package com.thssh.ui.lsn16.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TimeInterpolator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.thssh.ui.R;

public class AddCartActivity extends AppCompatActivity {

    private ImageView iv_cart;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_cart);
        iv_cart = (ImageView) findViewById(R.id.iv_cart);
    }

    /**
     * x: 匀速 x = vt
     * y: 自由落体 y = vt = 1/2*g*t*t
     * 估值器：可以再执行的过程中干预改变属性的值
     *
     * @param view
     */
    public void addCartClick(final View view) {
        ValueAnimator transAnimator = new ValueAnimator();
        transAnimator.setDuration(1000);
        transAnimator.setObjectValues(new PointF(0, 0), new PointF(iv_cart.getLeft(), iv_cart.getTop()));
        final PointF pointF = new PointF();
        transAnimator.setEvaluator(new TypeEvaluator<PointF>() {
            @Override
            public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
                float dx = endValue.x - startValue.x;
                pointF.x = startValue.x + dx * fraction;

                float g = 9.8f;
                float dy = endValue.y - startValue.y;
                float t = (float) Math.sqrt(2*dy/g) * fraction;
                pointF.y = startValue.y + 0.5f * 9.8f * t * t;
                return pointF;
            }
        });
        transAnimator.setInterpolator(new TimeInterpolator() {
            @Override
            public float getInterpolation(float input) {
                return 0;
            }
        });
        transAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF value = (PointF) animation.getAnimatedValue();
                view.setX(value.x);
                view.setY(value.y);
            }
        });
        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0.5f, 1.0f);
        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0.5f, 1.0f);
        float x = iv_cart.getLeft();
        float y = iv_cart.getTop();
        PropertyValuesHolder translationX = PropertyValuesHolder.ofFloat("translationX", x, x-20, x);
        PropertyValuesHolder translationY = PropertyValuesHolder.ofFloat("translationY", y, y-20, y);
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, translationY);
        objectAnimator.setDuration(200);
        AnimatorSet animatorSet = new AnimatorSet();
//        animatorSet.setDuration(1500);
        animatorSet.playSequentially(transAnimator, objectAnimator);

        animatorSet.start();
    }
}
